﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonsLib.Utility;
using System.Data.SqlClient;
using MaxLeaf.Platform.Functions;
using System.Data.Common;
using MaxLeaf.Platform.Commons;
using CommonsLib.DB;
using MaxLeaf.Platform.DataContract;

namespace MaxLeaf.Platform.DALImplementation
{
    public class imOthersOutputHdrDAL : BaseDAL
    {

        protected override string GetCondForPage(DBAccess dbcon, object model, List<Cond> lstCond)
        {
            string strCond = base.GetCondForPage(dbcon, model, lstCond);

            if (lstCond != null)
            {
                foreach (Cond cond in lstCond)
                {
                    if (cond.PropName.Equals("Func_CheckReqUserName", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (!string.IsNullOrEmpty(strCond))
                        {
                            strCond += " and ";
                        }
                        strCond += string.Format("  exists (select a.userid from {0}muser_main a where a.userid=ReqUserID and a.NickName like '{1}')"
                            , GetBaseDatabase()
                                , BaseFunc.SpecialCharDeal(CvtFunc.ToString(cond.Value))
                            );
                        continue;
                    }
                    if (cond.PropName.Equals("Func_CheckEnterUserName", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (!string.IsNullOrEmpty(strCond))
                        {
                            strCond += " and ";
                        }
                        strCond += string.Format("  exists (select a.userid from {0}muser_main a where a.userid=EnterUserID and a.NickName like '{1}')"
                            , GetBaseDatabase()
                                , BaseFunc.SpecialCharDeal(CvtFunc.ToString(cond.Value))
                            );
                        continue;
                    }
                    if (cond.PropName.Equals("Func_CheckApproUserName", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (!string.IsNullOrEmpty(strCond))
                        {
                            strCond += " and ";
                        }
                        strCond += string.Format("  exists (select a.userid from {0}muser_main a where a.userid=ApproUserID and a.NickName like '{1}')"
                            , GetBaseDatabase()
                                , BaseFunc.SpecialCharDeal(CvtFunc.ToString(cond.Value))
                            );
                        continue;
                    }
                }
            }
            return strCond;
        }

        public void SetPosted(CommonsLib.DB.DBAccess dbcon, int intHdrID)
        {
            string strSql = @"update imOthersOutputHdr set posted=1,DocStatus=1  where FInterID=@FInterID and posted=0 
                and not exists (select hdrid from imOthersOutputDtl where HdrID=@FInterID and posted=0) ";
            dbcon.ExecuteSql(strSql, new SqlParameter[] { new SqlParameter("@FInterID", intHdrID.ToString())
 
            });
        }

        public void SetUnRecv(DBAccess dbcon, int intHdrID)
        {
            string strSql = @"update imOthersOutputHdr set is_delete=1  where FInterID=@FInterID and posted=0 
                ";
            dbcon.ExecuteSql(strSql, new SqlParameter[] { new SqlParameter("@FInterID", intHdrID.ToString())
 
            });
        }
    }
}
